//
//  FavoriteDetailViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14-9-12.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "FavoriteDetailViewController.h"
#import "UIImageView+MJWebCache.h"
#import "myData.h"
#import "myObj.h"

@interface FavoriteDetailViewController ()

@end

@implementation FavoriteDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {
    [MobClick beginLogPageView:@"FavoriteDetailViewController"];
}
-(void)viewDidAppear:(BOOL)animated {
    [MobClick endLogPageView:@"FavoriteDetailViewController"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self getData];
    UILabel *titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
    titelLabel.text = @"菜式详解";
    titelLabel.textAlignment = NSTextAlignmentCenter;
    titelLabel.font = [UIFont systemFontOfSize:19];
    titelLabel.textColor = [UIColor colorWithRed:0.97 green:0.69 blue:0.17 alpha:1];
    self.navigationItem.titleView = titelLabel;
//    myData *mydata=[[myData alloc]init];
//    [mydata initData];
//    NSArray *array = [mydata selectData];
//    if (array.count == 0) {
//        UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(buttonClick)];
//        self.navigationItem.rightBarButtonItem = right;
//        _isExist = NO;
//    } else {
//        for (int i = 0; i < array.count; i++) {
//            myObj *obj = [array objectAtIndex:i];
//            if ([obj.nameString isEqualToString:_name]) {
//                UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(buttonClick)];
//                self.navigationItem.rightBarButtonItem = right;
//                _isExist = YES;
//            } else {
//                UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(buttonClick)];
//                self.navigationItem.rightBarButtonItem = right;
//                _isExist = NO;
//            }
//        }
//    }

    // Do any additional setup after loading the view.
}
-(void)getData {
    _makeImg = [[NSMutableArray alloc]init];
    _makeInfo = [[NSMutableArray alloc]init];
    _materialQuantity = [[NSMutableArray alloc]init];
    _materialArray = [[NSMutableArray alloc]init];
    _auxiliaryMaterial = [[NSMutableArray alloc]init];
    _auxiliaryQuantity = [[NSMutableArray alloc]init];
    
    myData *mydata=[[myData alloc]init];
    [mydata initData];
    NSArray *array1 = [mydata selectHowToDo:@"howToDo" :_dishName];
    for (int i = 0; i < array1.count; i++) {
        myObj *obj = [array1 objectAtIndex:i];
        [_makeImg addObject:obj.passNum];
        [_makeInfo addObject:obj.tips];
    }
    NSArray *array2 = [mydata selectHowToDo:@"mainMaterial" :_dishName];
    for (int i = 0; i < array2.count; i++) {
        myObj *obj = [array2 objectAtIndex:i];
        [_materialArray addObject:obj.passNum];
        [_materialQuantity addObject:obj.tips];
    }
    NSArray *array3 = [mydata selectHowToDo:@"auxiliaryMaterial" :_dishName];
    for (int i = 0; i < array3.count; i++) {
        myObj *obj = [array3 objectAtIndex:i];
        [_auxiliaryMaterial addObject:obj.passNum];
        [_auxiliaryQuantity addObject:obj.tips];
    }
    [self creatController];
}
-(void)creatController {
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, kHeight)];
    _scroll.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scroll];
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(174, 50, 200, 30)];
    name.font = [UIFont systemFontOfSize:22];
    name.text = _dishName;
    [name sizeToFit];
    [_scroll addSubview:name];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 10, 150, 110)];
    [image setImageURLStr:_bigImg placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
    [_scroll addSubview:image];
    
    UIImageView *image1 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 130, 320, 32)];
    image1.image = [UIImage imageNamed:@"img_jianjie"];
    [_scroll addSubview:image1];
    
    UILabel *introduction = [[UILabel alloc]initWithFrame:CGRectMake(10, 172, 300, 20)];
    introduction.text = [NSString stringWithFormat:@"       %@",_introduction];
    introduction.font = [UIFont systemFontOfSize:13];
    introduction.numberOfLines = 0;
    [introduction sizeToFit];
    [_scroll addSubview:introduction];
    
    UIImageView *image2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, introduction.frame.size.height + introduction.frame.origin.y + 16, 320, 32)];
    image2.image = [UIImage imageNamed:@"img_zhuliao"];
    [_scroll addSubview:image2];
    
    _materialTable = [[UITableView alloc]initWithFrame:CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, 320, 200)];
    if (_materialArray.count % 2 == 1) {
        _materialTable.frame = CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, 320, (_materialArray.count / 2 + 1) * 50 );
    } else {
        _materialTable.frame = CGRectMake(0, image2.frame.size.height + image2.frame.origin.y, 320, (_materialArray.count / 2) * 50 );
    }
    _materialTable.scrollEnabled = NO;
    _materialTable.delegate = self;
    _materialTable.dataSource = self;
    _materialTable.tag = 1;
    _materialTable.rowHeight = 50;
    [_scroll addSubview:_materialTable];
    
    UIImageView *image3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _materialTable.frame.origin.y + _materialTable.frame.size.height - 1, 320, 32)];
    image3.image = [UIImage imageNamed:@"img_fuliao"];
    [_scroll addSubview:image3];
    
    _auxiliaryTable = [[UITableView alloc]initWithFrame:CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, 320, 200)];
    if (_auxiliaryMaterial.count % 2 == 1) {
        _auxiliaryTable.frame = CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, 320, (_auxiliaryMaterial.count / 2 + 1) * 50 );
    } else {
        _auxiliaryTable.frame = CGRectMake(0, image3.frame.origin.y + image3.frame.size.height, 320, (_auxiliaryMaterial.count / 2) * 50 );
    }
    _auxiliaryTable.scrollEnabled = NO;
    _auxiliaryTable.delegate = self;
    _auxiliaryTable.dataSource = self;
    _auxiliaryTable.tag = 3;
    _auxiliaryTable.rowHeight = 50;
    [_scroll addSubview:_auxiliaryTable];
    
    _image4 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _auxiliaryTable.frame.size.height + _auxiliaryTable.frame.origin.y - 1, 320, 32)];
    _image4.image = [UIImage imageNamed:@"img_zhizuo"];
    [_scroll addSubview:_image4];
    
    _startTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 32 + _image4.frame.origin.y, 320, _makeInfo.count * 70)];
    _startTable.scrollEnabled = NO;
    _startTable.delegate = self;
    _startTable.dataSource = self;
    _startTable.tag = 2;
    _startTable.rowHeight = 70;
    _startTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_scroll addSubview:_startTable];
    
    UIImageView *image5 = [[UIImageView alloc]initWithFrame:CGRectMake(0, _startTable.frame.origin.y + 16+ _startTable.frame.size.height - 1, 320, 32)];
    image5.image = [UIImage imageNamed:@"img_xiaotieshi"];
    [_scroll addSubview:image5];
    
    UILabel *tips = [[UILabel alloc]initWithFrame:CGRectMake(10,15 + image5.frame.origin.y + image5.frame.size.height, 290, 100)];
    tips.text = [NSString stringWithFormat:@"       %@", _tips];
    tips.numberOfLines = 0;
    tips.font = [UIFont systemFontOfSize:14];
    [tips sizeToFit];
    [_scroll addSubview:tips];
    
    UIImageView *back = [[UIImageView alloc]initWithFrame:CGRectMake(0,10 + tips.frame.size.height + tips.frame.origin.y, 320, 25)];
    back.image = [UIImage imageNamed:@"img_back"];
    [_scroll addSubview:back];
    
    UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, back.frame.size.height + back.frame.origin.y, 320, kHeight)];
    background.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
    [_scroll addSubview:background];
    
    _scroll.contentSize = CGSizeMake(320, back.frame.size.height + back.frame.origin.y + 64);
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 1) {
        if (_materialArray.count % 2 == 1) {
            return _materialArray.count / 2 + 1;
        } else {
            return _materialArray.count / 2;
        }
    } else if (tableView.tag == 2) {
        return _makeImg.count;
    } else if (tableView.tag == 3) {
        if (_auxiliaryMaterial.count % 2 == 1) {
            return _auxiliaryMaterial.count / 2 + 1;
        } else {
            return _auxiliaryMaterial.count / 2;
        }
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (tableView.tag == 1) {
        if (_materialArray.count % 2 == 1) {
            if (indexPath.row == _materialArray.count / 2) {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_materialArray objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
                quantity1.text = [_materialQuantity objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
            } else {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_materialArray objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
                quantity1.text = [_materialQuantity objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
                
                UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(184, 10, 70, 30)];
                material2.text = [_materialArray objectAtIndex:indexPath.row * 2 + 1];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material2];
                
                UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(228, 10, 70, 30)];
                quantity2.text = [_materialQuantity objectAtIndex:indexPath.row * 2 + 1];
                quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity2.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity2];
            }
        } else {
            UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
            material1.text = [_materialArray objectAtIndex:indexPath.row * 2];
            material1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material1];
            
            UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(90, 10, 70, 30)];
            quantity1.text = [_materialQuantity objectAtIndex:indexPath.row * 2];
            quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity1];
            
            UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(184, 10, 70, 30)];
            material2.text = [_materialArray objectAtIndex:indexPath.row * 2 + 1];
            material2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material2];
            
            UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(228, 10, 70, 30)];
            quantity2.text = [_materialQuantity objectAtIndex:indexPath.row * 2 + 1];
            quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity2];
        }
    } else if (tableView.tag == 2) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(7, 5, 80, 60)];
        [image setImageURLStr:[_makeImg objectAtIndex:indexPath.row] placeholder:[UIImage imageNamed:@"img_delete_advertisement"]];
        image.clipsToBounds = YES;
        image.contentMode = UIViewContentModeScaleAspectFill;
        [cell.contentView addSubview:image];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(92, 5, 215, 55)];
        label.text = [NSString stringWithFormat:@"%d.%@",indexPath.row + 1,[_makeInfo objectAtIndex:indexPath.row]];
        label.font = [UIFont systemFontOfSize:14];
        label.numberOfLines = 4;
        [cell.contentView addSubview:label];
    } else if (tableView.tag == 3) {
        if (_auxiliaryMaterial.count % 2 == 1) {
            if (indexPath.row == _auxiliaryMaterial.count / 2) {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
                quantity1.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                quantity1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:quantity1];
            } else {
                UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
                material1.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2];
                material1.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material1];
                
                UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
                quantity1.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2];
                quantity1.font = [UIFont systemFontOfSize:15];
                quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                [cell.contentView addSubview:quantity1];
                
                UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(186, 10, 70, 30)];
                material2.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2 + 1];
                material2.font = [UIFont systemFontOfSize:15];
                [cell.contentView addSubview:material2];
                
                UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
                quantity2.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2 + 1];
                quantity2.font = [UIFont systemFontOfSize:15];
                quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
                [cell.contentView addSubview:quantity2];
            }
        } else {
            UILabel *material1 = [[UILabel alloc]initWithFrame:CGRectMake(29,10,70,30)];
            material1.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2];
            material1.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material1];
            
            UILabel *quantity1 = [[UILabel alloc]initWithFrame:CGRectMake(94, 10, 70, 30)];
            quantity1.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2];
            quantity1.font = [UIFont systemFontOfSize:15];
            quantity1.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            [cell.contentView addSubview:quantity1];
            
            UILabel *material2 = [[UILabel alloc]initWithFrame:CGRectMake(186, 10, 70, 30)];
            material2.text = [_auxiliaryMaterial objectAtIndex:indexPath.row * 2 + 1];
            material2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:material2];
            
            UILabel *quantity2 = [[UILabel alloc]initWithFrame:CGRectMake(251, 10, 70, 30)];
            quantity2.text = [_auxiliaryQuantity objectAtIndex:indexPath.row * 2 + 1];
            quantity2.textColor = [UIColor colorWithRed:0.41 green:0.4 blue:0.4 alpha:1];
            quantity2.font = [UIFont systemFontOfSize:15];
            [cell.contentView addSubview:quantity2];
        }
    }
    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
